<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/8/2 0002
 * Time: 16:02
 */

namespace app\admin\controller;


use app\model\model\User_comment;
use think\Request;

class Usercomm extends Base
{
    protected $_User;
    public function __construct(Request $request = null){
        parent::__construct($request);
        $this->_User = new User_comment();
    }
    public function lists(){
        return $this->fetch();
    }
    public function getList(){
        $where = [];
        $name = input('name');
        if($name){
            $where['name'] = ['like',"%$name%"];
        }
        $data = $this->_User->getList($where,input('limit'));
        foreach ($data as $v){
             $v['img_url']='<img style="height: 50px;width: 80px;" src="'.$v['img_url'].'">';
            $v['create_time'] = date('Y-m-d H:i:s',$v['create_time']);
        }
        $list = $data->toArray();
        echo json_echo($list);
    }
    /**
     * 添加
     * */
    public function add(){
        if(!Request::instance()->post()){
            return $this->fetch();
        }else{

            $data = [
                'name'=>input('name'),
                'content'=>input('content'),
                'img_url'=>input('img_url'),
                'create_time'=>time(),
            ];
            $re = $this->_User->add($data);
            if($re){
                return json_echo_msg(200,'添加成功');
            }else{
                return json_echo_msg(500,'添加失败');
            }
        }
    }
    /**
     * 修改
     * */
    public function up(){
        if(!Request::instance()->post()){
            $data = $this->_User->getOne(input('id'));
            $this->assign('data',$data);
            return $this->fetch();
        }else{
            $data = [
                'name'=>input('name'),
                'content'=>input('content'),
            ];
            if(input('img_url')){
                $data['img_url'] = input('img_url');
            }
            $re = $this->_User->up(['id'=>input('id')],$data);
            if($re){
                return json_echo_msg(200,'修改成功');
            }else{
                return json_echo_msg(500,'修改失败');
            }
        }
    }
    /**
     * 获取一条数据
     * */
    public function getOne(){
        $data = $this->_User->getOne(input('id'));
        echo json_echo_msg(200,'成功',$data);
    }
    /**
     * 删除
     * */
    public function del(){
       
        $re = $this->_User->del(['id'=>input('id')]);
        if($re){
            return json_echo_msg(200,'删除成功！');
        }else{
            return json_echo_msg(400,'删除失败！');
        }
    }
    /**
     * 批量删除
     * */
    public function delAll(){
       
        $re = $this->_User->delAll(input()['data']);
        if($re){
            return json_echo_msg(200,'删除成功！');
        }else{
            return json_echo_msg(400,'删除失败！');
        }
    }

}